angular.module('starter')
.controller("paymentCtrl", function ($scope, CountDown, $ionicHistory,$ionicPopup,$stateParams, httpService) {
    var reInfo = $stateParams.reInfo || {};
    $scope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
        if (toState.url == "paymentRoute" && (fromState.url == "placeOrder"||fromState.url=="payment_methodRoute")) {
            initialize();
            return true;
        }
    });
 
	$scope.CancelPayFn=function()
	{
		if ($scope.DoctorParma.status == 'PAYED' && $scope.DoctorParma.regDate< (new Date().Format("yyyy-MM-dd")))
        {
            $ionicPopup.alert({
                title: '取消失败',
                template: '挂号当天不得取消订单！'
            });
            return;
        }
        
         //post请求取消支付订单
        var parData = {};
        parData.hospitalCode=$scope.Hospital.hspCode;
        parData.orderId=reInfo.orderId;
        parData.hisOrderId=reInfo.hisOrderId;
        parData.name="其他原因";
        parData.lock_no=reInfo.lockId;
  
        httpService.post(httpService.appWebApi.refundPreReg, parData).then(
           function (success) {
               var data = success.data;
               if (data.returnCode != "0") {
               	   $ionicPopup.alert({
		                title: '取消失败',
		                template: data.returnMsg
		            });
                   console.log(data.returnMsg)
                   return;
               }
               $scope.DoctorParma.status = 'CANCEL';
               CountDown.clear();
           }
           , function (error) {
               console.log(error);
           });

        return;
        
	}
    $scope.CancelFn = function ()
    {
        if ($scope.DoctorParma.status == 'PAYED' && $scope.DoctorParma.regDate< (new Date().Format("yyyy-MM-dd")))
        {
            $ionicPopup.alert({
                title: '取消失败',
                template: '挂号当天不得取消订单！'
            });
            return;
        }
        //post请求取消订单
        var parData = {};
        parData.hospitalCode=$scope.Hospital.hspCode;
        parData.orderId=reInfo.orderId;
        parData.hisOrderId=reInfo.hisOrderId;
        parData.name="其他原因";
        parData.lock_no=reInfo.lockId;
        var url = "";
        if ($scope.DoctorParma.type == "2") {
            url = httpService.appWebApi.cancelPreRegOrder;
        } else {
            url = httpService.appWebApi.unlockRegSeqToday;
        }
        httpService.post(url, parData).then(
           function (success) {
               var data = success.data;
               if (data.returnCode != "0") {
                   console.log(data.returnMsg)
                   return;
               }
               $scope.DoctorParma.status = 'CANCEL';
               CountDown.clear();
           }
           , function (error) {
               console.log(error);
           });

        return;
    }
 

    //初始化
    function initialize()
    {

        if ($ionicHistory.backView().stateName == "placeOrder"||
        $ionicHistory.backView().stateName == "payment_methodRoute")
        {
            $ionicHistory.removeBackView();
        }
        var orderData = angular.fromJson(localStorage.getItem("orderData")) || [];

       $scope.DoctorParma = orderData.DoctorParma;
        $scope.UserInfo = orderData.UserInfo;
        $scope.Hospital = orderData.Hospital;
        orderData.reInfo = reInfo;

        localStorage.setItem("orderData", angular.toJson(orderData));

        getLikeDoctor(4, $scope.DoctorParma.deptId, $scope.Hospital.hspCode);
        if ($scope.DoctorParma.status =='NEW') {
            CtimeDown(900, $("#countDown"));
        }
   

    }

    function CtimeDown(count,ele)
    {
        CountDown.SetCount(count);
        var ct = setInterval(function () {
            if (CountDown.CountValue() > 0) {
                $(ele).html(getLocalTime(CountDown.CountValue()));
            } else {
                $(ele).html("00:00");
                clearInterval(ct);
                if ($scope.DoctorParma.status == 'NEW') {
                    $scope.DoctorParma.status = 'TIMEOUT';
                    $scope.$apply();
                }
                return;
            }
        }, 1000)
    }

   //推荐类似医生数据
    function getLikeDoctor(top, deptId, hspId) {
       $scope.payment_doctor = [];
 
       var parData = "?page=" + top + "&hospitalCode=" + hspId + "&deptId=" + deptId;
       httpService.get(httpService.appWebApi.getLikeDoctor + parData
             ).then(
             function (success) {
                 var data = success.data;
                 if (data.returnCode != "0") {
                     console.log(data.returnMsg)
                     return;
                 }
                 angular.forEach(data.returnData, function (modal) {
                     $scope.payment_doctor.push(
                         {
                             "doctor_name": modal.doctorName,
                             "doctor_section": modal.deptName,
                             "doctor_position": modal.doctorTitle,
                             "hospitalName": modal.hospitalName,
                             "doctor_src": modal.urlPic || "img/face1.png"
                         });
                 });
             }
             , function (error) {
                 console.log(error);
             });
        }
});